package com.qf.dao.impl;

import com.qf.dao.NavigDao;
import com.qf.model.Navig;
import com.qf.model.PageBean;
import com.qf.model.User;
import com.qf.utils.DruidUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class NavigDaoImpl implements NavigDao {
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUtil.getDataSource());

    @Override
    public List<Navig> finNavig(int u_manager) {
        String sql1 = "";
        if (u_manager == 0) {
            sql1 = "select * from navig where ready1="+u_manager;
        } else {
            sql1 = "select * from navig";
        }

        List<Navig> list = jdbcTemplate.query(sql1, new BeanPropertyRowMapper<>(Navig.class));
        return list;
    }

    @Override
    public List<Navig> finNavigAll(PageBean<Navig> pageBean, String name) {
        String sql1 = "select * from navig where name like ?";
        List<Navig> list = jdbcTemplate.query(sql1, new BeanPropertyRowMapper<>(Navig.class), "%" + name + "%");
        pageBean.setCount(list.size());
        String sql = "select * from navig where name like ? limit ?,?";
        int i = (pageBean.getCurPage() - 1) * pageBean.getPageSize();
        List<Navig> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Navig.class), "%" + name + "%", i, pageBean.getPageSize());
        return query;
    }

    @Override
    public List<Navig> findNavigByID(int id) {
        String sql = "select * from navig where id=?";
        List<Navig> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Navig.class), id);
        return query;
    }

    @Override
    public int updateNavige(Navig navig) {
        String sql = "update navig set name=?,pid=?,url=? where id=?";
        int num = jdbcTemplate.update(sql, navig.getName(), navig.getPid(), navig.getUrl(), navig.getId());
        return num;
    }

    @Override
    public int addNavig(Navig navig) {
        String sql = "insert into navig(name,pid,url) values(?,?,?)";
        int num = jdbcTemplate.update(sql, navig.getName(), navig.getPid(), navig.getUrl());
        return num;
    }

    @Override
    public int deleteNavigByID(int id) {
        String sql = "delete  from navig where  id=?";
        int num = jdbcTemplate.update(sql, id);
        return num;
    }
}
